package com.example.robertluozizhao.framecollectdemo.view.custom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.example.robertluozizhao.framecollectdemo.R;

/**
 * @author luo_zi_zhao
 * @version V1.0 当前版本描述信息
 * @FileName com.example.robertluozizhao.framecollectdemo.view.custom
 * @date 2019/5/16 16:17
 */

public class WritePadView extends View {
    private static final String TAG = "WritePadView";
    private Paint mPaint;
    private Path mPath;
    private float startDrawX;
    private float startDrawY;
    private float endDrawX;
    private float endDrawY;

    public WritePadView(Context context) {
        this(context, null);
    }

    public WritePadView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    @SuppressLint("ResourceAsColor")
    private void initPaint() {
        mPaint = new Paint();
        mPaint.setStrokeWidth(10);
        mPaint.setColor(R.color.red);
        mPaint.setStyle(Paint.Style.STROKE);
//        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setAntiAlias(false);
        mPath = new Path();

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        //请求所有父控件及祖宗控件不要拦截事件
        getParent().requestDisallowInterceptTouchEvent(true);
        return super.dispatchTouchEvent(event);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.AT_MOST) {
            widthSize = 800;
        }

        if (heightMode == MeasureSpec.AT_MOST) {
            heightSize = 1200;
        }

        Log.d(TAG, "onMeasure: " + "widthSize:" + widthSize + ",heightSize:" + heightSize);
        setMeasuredDimension(widthSize, heightSize);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startDrawX = event.getX();
                startDrawY = event.getY();
                mPath.moveTo(startDrawX,startDrawY);
            case MotionEvent.ACTION_MOVE:
                endDrawX = event.getX();
                endDrawY = event.getY();
                mPath.lineTo(endDrawX,endDrawY);
                postInvalidate();
                break;
            case MotionEvent.ACTION_UP:
                break;
        }

        return true;
    }


    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        canvas.drawPath(mPath,mPaint);
    }

    public void reset(){
        if (mPaint!=null) {
            mPath.reset();
            mPaint.reset();
            initPaint();
            invalidate();
        }
    }
}
